<?php $this->load->view("admin/$adminCurrentTemplate/commonHead2");?>
<table width="99%" border="0" cellspacing="1" cellpadding="0"
	class="editFormContainer">
	<form id="editForm" name="editForm" method="post" action="<?=$this->input->server('REQUEST_URI')?>">
	<input type="hidden" name="id" id="id" value="<?php if(isset($id)) echo $id; ?>" />
	<tr>
		<td class="guid">字段类型：</td>
		<td class="formItem">
			<select name='autofield' id='autofield' size='1' class="single-select">
				<option value='1'<?php if(isset($autofield)) if($autofield==1) echo ' selected'?>>自动表单</option>
				<option value='0'<?php if(isset($autofield)) if($autofield==0) echo ' selected'?>>固化字段</option>				
			</select>
			<span class='guid' style='margin-left:20px;'>是否允许为空：</span><select name='isnull' id='isnull' size='1' class='single-select'>
				<option value='1'<?php if(isset($isnull)) if($isnull==1) echo ' selected'?>>允许为空</option>
				<option value='0'<?php if(isset($isnull)) if($isnull==0) echo ' selected'?>>不允许为空</option>
			</select>
		</td>
	</tr>
	<tr>
		<td class="guid">数据类型：</td>
		<td class="formItem">
			<select name='type' id='type' size='1' class="single-select">
				<?php foreach($datatype as $datatype_key=>$datatype_value){?>
				<option value='<?php echo $datatype_key?>'<?php if(isset($type)) if($type==$datatype_key) echo ' selected'?>><?php echo $datatype_value?></option>
				<?php } ?>
			</select>
		</td>
	</tr>
	<tr>
		<td class="guid">表单提示文字：</td>
		<td class="formItem">
			<input type="text" name="itemname" id="itemname" class="sing-line input-name" value="<?php if(isset($itemname)) echo $itemname;?>" />
		</td>
	</tr>
	<tr>
		<td class="guid">字段名称：</td>
		<td class="formItem"><input type="text" name="fieldname"
			id="fieldname" class="sing-line input-name"
			value="<?php if(isset($fieldname)) echo $fieldname;?>"<?php if(isset($autofield)) if($autofield==0) echo ' readonly'?>/></td>
	</tr>
	<tr>
		<td class="guid">最大长度：</td>
		<td class="formItem"><input type="text" name="maxlength"
			id="maxlength" class="sing-line input-name"
			value="<?php if(isset($maxlength)) echo $maxlength;?>" /></td>
	</tr>
	<tr>
		<td class="guid">默 认 值：</td>
		<td class="formItem">
			<div style="line-height:14px; background:#efefef;">如果定义数据类型为select、radio、checkbox时，此处填写被选择的项目(用“,”分开，如“男,女,人妖”)</div>
			<textarea name="default" id="default" class="multi-line" style='height:86px;'><?php if(isset($default)) echo $default;?></textarea>
		</td>
	</tr>
	</form>
</table>
<?php $this->load->view("admin/$adminCurrentTemplate/commonFoot2");?>
<script type="text/javascript">
$(function(){
	dg.addBtn('subBtn','提 交',subForm);
	$('#fieldname').bind('blur',fieldNameBlur);
	var oldFieldName = '<?php echo $this->uri->segment(5);?>';
	$('#type').bind('change',typeChange);
	if(oldFieldName.length==0) dg.SetTitle('模型字段配置(可视化模式)——添加字段');
})

var fieldNameBlur = function(){
	var id = $('#id').val();
	var thisValue = $.trim($(this).val());
	if(thisValue.toLowerCase()=='aid' || thisValue.toLowerCase()=='channelid' || thisValue.toLowerCase()=='categoryid'){
		$('#fieldname').val('');
		subMess(268,118,'附加表字段错误','<span class="red" style="line-height:20px;">对不起，<b>aid</b>、<b>channelid</b>、<b>categoryid</b><br />系统会自动添加，不能手动添加！</span>',$("#fieldname"));
		return false;
	}
	if(thisValue.length>0){
		$.ajax({
			type:'POST',
			url:getInfo + '/checkField/',
			data:'oldName=<?php echo $this->uri->segment(5);?>&fieldName=' + thisValue + '&id=' + id,
			success:function(returnValue){
				if(parseInt(returnValue)>0){
					subMess(218,108,'附加表字段错误','<span class="red">对不起，字段名称已经存在！</span>',$("#fieldname"));
					return false;
				}
			}
		});
	}
}

var typeChange = function(){
	
}

var subForm = function(){
	//dg.iDoc('editMess').getElementById('fieldset').value = '';
	//$('#fieldset',dg.iDoc('editMess')).val('');
	//dg.iWin('editMess').location.reload();
	//dg.cancel();
	
	var itemname = $.trim($('#itemname').val());
	if(itemname.length==0){
		subMess(380,108,'表单提示文字错误','<span class="red">对不起，表单提示文字不能为空，请输入表单提示文字！</span>',$("#itemname"));
		return false;
	}
	var fieldname = $.trim($('#fieldname').val());
	if(fieldname.length==0){
		subMess(320,108,'附加表字段错误','<span class="red">对不起，字段名称不能为空，请输入字段名称！</span>',$("#fieldname"));
		return false;
	}
	var typeValue = $.trim($('#type').val());
	var maxlength = $.trim($('#maxlength').val());
	var defaultValue = $.trim($('#default').val());
	switch(typeValue){
		case 'text':
		case 'textchar':
		case 'multitext':
		case 'img':
		case 'imgfile':
		case 'media':
		case 'addon':
			if(maxlength.length==0){
				subMess(320,108,'附加表字段错误','<span class="red">对不起，最大长度不能为空，请输入最大长度！</span>',$("#maxlength"));
				return false;
			}else if(!isNumber(maxlength)){
				$("#maxlength").val('');
				subMess(320,108,'附加表字段错误','<span class="red">对不起，最大长度必须为数字，请重新输入！</span>',$("#maxlength"));
				return false;
			}else if(parseInt(maxlength)<=0 || parseInt(maxlength)>255){
				subMess(340,108,'附加表字段错误','<span class="red">对不起，最大长度必须在1~255之间，请重新输入！</span>',$("#maxlength"));
				return false;
			}
			break;
		case 'int':
		case 'float':
			if(!isNumber(defaultValue)){
				subMess(340,108,'附加表字段错误','<span class="red">对不起，您所选择数据类型中，默认值必须为数字！</span>',$("#default"));
				return false;
			}
			break;
		case 'select':
		case 'radio':
		case 'checkbox':
			if(defaultValue.length==0){
				subMess(320,118,'附加表字段错误','<span class="red" style="line-height:20px;">对不起，您选择的数据类型中必须设置选项！<br />请在默认值对应的输入框中设置您的可供选项！</span>',$("#default"));
				return false;
			}
			break;
	}
	$('#editForm').submit();
}
</script>